package com.wordsay.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wordsay.common.Constant;
import com.wordsay.entity.People;
import com.wordsay.entity.Talk;

public class TalkUtil {

	//.*\s\([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\)˵:
	
	/**
	 * distinguish the message whether is a new talk
	 * @param message
	 * @return
	 */
	public static boolean isFxNewTalk(String message) {
		
		
		Pattern pattern = Pattern.compile(".*\\s\\([0-9]{4}-[0-9]{2}-[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\)说:");
		Matcher matcher = pattern.matcher(message);
		
		return matcher.find();
	}
	
	/**
	 * parse a feixin message as a talk object
	 * @param message
	 * @return
	 */
	public static Talk parseFxMessage(String message) throws Exception{
		
		if(message == null || "".equals(message.trim())){
			
			return null;
		}
		//饶平平	(2013-05-03 09:48:39)说:  你咋不早说捏

		Talk talk = new Talk();
		
		int indexOfLeftBrackets = message.indexOf("(");
		int indexOfRightBrackets = message.indexOf(")");
		
		
		String speakerName = new String(message.substring(0,indexOfLeftBrackets-1).trim());
		String speakTime = new String(message.substring(indexOfLeftBrackets+1,indexOfRightBrackets));
		Calendar datetime = null;
		try{
			
			 datetime = DateTimeUtil.converToCalender(speakTime);
		}catch(Exception e){
			indexOfLeftBrackets = message.indexOf("(",indexOfLeftBrackets);
			indexOfRightBrackets = message.indexOf(")",indexOfLeftBrackets);
			speakTime = new String(message.substring(indexOfLeftBrackets+1,indexOfRightBrackets));
			try{
				 datetime = DateTimeUtil.converToCalender(speakTime);
			}catch(Exception e1){
				return null;
			}
		}
		
		System.out.println(message+" --> "+speakTime+"   -"+speakerName);
		
		String talkMessage = message.substring(message.indexOf("说")+2).trim();
		
		People speaker = new People();
		speaker.setName(speakerName);
		
		talk.setSpeaker(speaker);
		talk.setTime(datetime);
        talk.appendMessage(talkMessage);
		
		return talk;
	}
	
	/**
	 * paser the talk id from chat record filename
	 * @param fileName
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static String parseTalkId(String fileName,int type) throws Exception{
		
		int indexOfLeftBrackets = fileName.indexOf("(");
		
		if(type == Constant.PEOPLE_TALK){
			
			return new String(fileName.substring(indexOfLeftBrackets,fileName.indexOf(")") - indexOfLeftBrackets));
			
		}else if(type == Constant.GROUP_TALK || type == Constant.DISCUSSION_TALK) {
			
		   int indexOfAt = fileName.indexOf("@"); 
		   return new String(fileName.substring(indexOfLeftBrackets,indexOfAt).replaceAll("sip[DP]G","").trim());
		
		}else{
			throw new Exception("talk type error:"+type);
		}
	}
	
	/**
	 * read the stop words
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public static Map<String,String> readStopWords(String filePath) throws Exception {
		
		Map<String,String> stopWords = new HashMap<String,String>();
		

		File stopWordsFile = new File(filePath);
		
		if(!stopWordsFile.exists()){
			
			return stopWords;
		}
		
		BufferedReader reader = new BufferedReader(new FileReader(stopWordsFile));
		String line = null;
		while((line = reader.readLine()) != null) {
			
			stopWords.put(line.trim(), null);
		}
		
		return stopWords;
		
	}
	
}
